import Vue from 'vue'
import VueCompositionApi from '@vue/composition-api'
import App from './App.vue'
import store from './store'
import router from './router'
import ElementUI from 'element-ui'
import './theme/index.scss'

import SvgIcon from './components/SvgIcon'
import scssVariables from './theme/variables.scss.js'
import axios from './lib/axios'

import './lib/globalFilters'

// import './mock/index'

Vue.config.productionTip = false
Vue.use(VueCompositionApi)
Vue.use(ElementUI)
Vue.component('SvgIcon', SvgIcon)

Vue.prototype.$cssVar = scssVariables
Vue.prototype.$axios = axios

window.globalConfig = {
  apiUrl: '',
}

Vue.mixin({
  methods: {
    copyObject (object) {
      return JSON.parse(JSON.stringify(object))
    },
    saveAdminInfo (info) {
      localStorage.setItem('jdp_admin_info', JSON.stringify(info))
    },
    getAdminInfo () {
      const str = localStorage.getItem('jdp_admin_info')
      // eslint-disable-next-line eqeqeq
      if (str != undefined && str != null && str.length > 0) {
        return JSON.parse(str)
      }
      return {}
    },

    formatDate (time, format = 'YY-MM-DD hh:mm:ss') {
      /**
       *  formatDate(new Date().getTime());//2017-05-12 10:05:44
       *  formatDate(new Date().getTime(),'YY年MM月DD日');//2017年05月12日
       *  formatDate(new Date().getTime(),'今天是YY/MM/DD hh:mm:ss');//今天是2017/05/12 10:07:45
       */
      var newTime
      if (!time) newTime = ''
      else {
        var date = new Date(time)
        var year = date.getFullYear()
        var month = date.getMonth() + 1
        var day = date.getDate()
        var hour = date.getHours()
        var min = date.getMinutes()
        var sec = date.getSeconds()
        var preArr = Array.apply(null, Array(10)).map(function (elem, index) {
          return '0' + index
        }) // 开个长度为10的数组 格式为 00 01 02 03

        newTime = format
          .replace(/YY/g, year)
          .replace(/MM/g, preArr[month] || month)
          .replace(/DD/g, preArr[day] || day)
          .replace(/hh/g, preArr[hour] || hour)
          .replace(/mm/g, preArr[min] || min)
          .replace(/ss/g, preArr[sec] || sec)
      }

      return newTime
    },
    /**
     * 校验form
     * func 回调函数
     */
    onSubmitForm (formName, func) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (func) func()
        } else {
          return false
        }
      })
    },
    /**
     * 重置form
     * func 回调函数
     */
    onResetForm (formName, func) {
      this.$refs[formName].resetFields()
      if (func) func()
    },
    /**
     * 解析字符串
     */
    evalData (v) {
      // eslint-disable-next-line no-eval
      return eval(v)
    },
    /**
     * 分转为元
     * @param {*} value
     */
    changePrice (value) {
      if (!value) value = 0.0
      else value = (parseFloat(value * 1000) / 100 / 1000).toFixed(2)
      return value
    },
  },
})

new Vue({
  store,
  router,
  ...App,
}).$mount('#app')
